package org.haxenme.sqlitetest;


import nme.display.Sprite;
import nme.display.StageAlign;
import nme.display.StageScaleMode;
import nme.Lib;
import nme.text.TextField;
import nme.text.TextFormat;
import cpp.db.Sqlite;


/**
 * @author Joshua Granick
 */
class SQLiteTest extends Sprite {
	
	
	private var Label:TextField;
	
	
	public function new () {
		
		super ();
		
		initialize ();
		construct ();
		
		var connection = Sqlite.open ("Users.db");
		
		connection.request ("
				CREATE TABLE IF NOT EXISTS User (
					id INTEGER PRIMARY KEY AUTOINCREMENT, 
					name TEXT, 
					age INTEGER, 
					money DOUBLE
				)
		");
		
		connection.request ("INSERT INTO User (name,age,money) VALUES ('John',32,100.45)");
		connection.request ("INSERT INTO User (name,age,money) VALUES ('Bob',14,4.50)");
		
		var resultSet = connection.request ("SELECT * FROM User");
		
		Label.appendText ("Found " + resultSet.length + " users\n");
		
		for (row in resultSet) {
			
			Label.appendText ("User " + row.name + " is " + row.age + " years old\n");
			
		}
		
		connection.close ();
		
	}
	
	
	private function construct ():Void {
		
		Label.defaultTextFormat = new TextFormat ("_sans", 24, 0x222222);
		Label.width = 400;
		Label.height = 400;
		Label.x = 10;
		Label.y = 20;
		Label.selectable = false;
		addChild (Label);
		
	}
	
	
	private function initialize ():Void {
		
		Lib.current.stage.align = StageAlign.TOP_LEFT;
		Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
		
		Label = new TextField ();
		
	}
	
	
	
	
	// Entry point
	
	
	
	
	public static function main () {
		
		Lib.current.addChild (new SQLiteTest ());
		
	}
	
	
}